package dao

import (
	"fmt"
	"strings"
	"yundian/internel/app/helper/request"
	"yundian/internel/app/models/orderoperatelog"
	"yundian/internel/app/models/placemodel"
	"yundian/internel/app/models/userprofitlog"
	"yundian/internel/pkg/dingo"
	"yundian/internel/pkg/dingo/global"
)

// 新增分成修改日志表
func (d *UserDao) InsertUserProfitLog(m *userprofitlog.UserProfitLog) *dingo.ErrorWrap {
	db := global.Orm.Table("user_profit_log").
		Select("create_at", "parent_id", "user_id", "profit_proportion",
			"user_name", "description", "change", "remark", "operation_user_id",
			"operation_user_name", "is_place", "before_field", "after_field", "operate_ip").
		Create(m)
	return dingo.ErrorIf(db.Error, true)
}

//查询分成修改日志列表
func (d *UserDao) GetUserProfitLog(param *request.UserProfitLogReq) *dingo.ErrorWrap {
	var (
		lst    []userprofitlog.UserProfitLog
		count  int64
		limit  int = param.PageSize
		offset int = param.PageSize * (param.PageNo - 1)
	)
	db := global.Orm.Debug().Table("user_profit_log l").
		Select("l.*,u.user_name as parent_user_name,u.name as parent_name").
		Joins("LEFT JOIN users u ON l.parent_id = u.id")

	if param.UserName != "" { db.Where("l.user_name like ?", fmt.Sprint("%", param.UserName, "%")) }
	if param.OperationUserName != "" { db.Where("l.operation_user_name like ?", fmt.Sprint("%", param.OperationUserName, "%")) }
	if param.BeginTime != "" {
		beginTime := strings.Split(param.BeginTime, ",")
		db.Where("l.create_at >= ? and l.create_at <= ?", beginTime[0], beginTime[1])
	}
	db.Order("l.create_at desc")
	db.Limit(limit).Offset(offset).Find(&lst).Limit(-1).Offset(-1).Count(&count)
	return dingo.ErrorIf(db.Error, lst, count)
}

//查询订单修改日志列表
func (d *UserDao) GetOrderOperateLog(param *request.OrderOperateLogReq) *dingo.ErrorWrap {
	var (
		lst    []orderoperatelog.OrderOperateLog
		count  int64
		limit  int = param.PageSize
		offset int = param.PageSize * (param.PageNo - 1)
	)
	db := global.Orm.Table("order_operate_log ")

	//用户名称
	if param.UserName != "" {
		db.Where("user_name like ?", fmt.Sprint("%", param.UserName, "%"))
	}

	// 时间查询
	if param.BeginTime != "" {
		beginTime := strings.Split(param.BeginTime, ",")
		fmt.Println("beginTime: ", beginTime[0], beginTime[1])
		db.Where("DATE_FORMAT(FROM_UNIXTIME(create_at),'%Y-%m-%d') >=  DATE_FORMAT(FROM_UNIXTIME(?),'%Y-%m-%d') and "+
			"DATE_FORMAT(FROM_UNIXTIME(create_at),'%Y-%m-%d')<=  DATE_FORMAT(FROM_UNIXTIME(?),'%Y-%m-%d')", beginTime[0], beginTime[1])
	}
	db.Order("create_at desc")
	//log.Println("GetPlaceList 执行的SQL: ",)
	db.Limit(limit).Offset(offset).Find(&lst).Limit(-1).Offset(-1).Count(&count)

	return dingo.ErrorIf(db.Error, lst, count)
}

// 新增分成修改日志表
func (d *UserDao) InsertPlaceOperateLog(m *placemodel.PlaceOperateLog) *dingo.ErrorWrap {
	db := global.Orm.Table("place_operate_log").Create(m)
	return dingo.ErrorIf(db.Error, true)
}

//查询分成修改日志列表
func (d *UserDao) GetPlaceOperateLog(param *request.PlaceOperateLogReq) *dingo.ErrorWrap {
	var (
		lst    []placemodel.PlaceOperateLog
		count  int64
		limit  int = param.PageSize
		offset int = param.PageSize * (param.PageNo - 1)
	)
	db := global.Orm.Debug().Table("place_operate_log")

	if param.PlaceName != "" { db.Where("place_name like ?", fmt.Sprint("%", param.PlaceName, "%")) }
	if param.OperationUserName != "" { db.Where("operation_user_name like ?", fmt.Sprint("%", param.OperationUserName, "%")) }
	if param.BeginTime != "" {
		beginTime := strings.Split(param.BeginTime, ",")
		db.Where("create_at >= ? and create_at <= ?", beginTime[0], beginTime[1])
	}

	db.Order("id desc")
	//db := global.Orm.Debug().Table("place_operate_log pol").
	//	Select("pol.*,p.place_name").
	//	Joins("left join places as p ON pol.place_id = p.id")
	//
	//if param.PlaceName != "" { db.Where("p.place_name like ?", fmt.Sprint("%", param.PlaceName, "%")) }
	//if param.OperationUserName != "" { db.Where("pol.operation_user_name like ?", fmt.Sprint("%", param.OperationUserName, "%")) }
	//if param.BeginTime != "" {
	//	beginTime := strings.Split(param.BeginTime, ",")
	//	db.Where("pol.create_at >= ? and pol.create_at <= ?", beginTime[0], beginTime[1])
	//}
	//
	//db.Order("pol.create_at desc")
	db.Limit(limit).Offset(offset).Find(&lst).Limit(-1).Offset(-1).Count(&count)
	return dingo.ErrorIf(db.Error, lst, count)
}
